:Init
@setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@pushd %~dp0
@prompt $_
@echo on
@if "%1" == "" goto Usage

:Params
if "%_RoleFeaturesXmlFile%"          == "" set _RoleFeaturesXmlFile=..\Schema\ServerManager.accelerator.xml
if "%_Site%"                         == "" set _Site=Default Web Site
if "%_UserAdmin%"                    == "" set _UserAdmin=%~2
if "%_UserAdminPassword%"            == "" set _UserAdminPassword=%~3
if "%_UserSharedConfig%"             == "" set _UserSharedConfig=
if "%_UserSharedCongifPassword%"     == "" set _UserSharedConfigPassword=
if "%_WebDavRequireSSL%"             == "" set _WebDavRequireSSL=false
if "%_WebDavAllowHiddenFiles%"       == "" set _WebDavAllowHiddenFiles=true
if "%_WebDavAllowNonMimeMapFiles%"   == "" set _WebDavAllowNonMimeMapFiles=true
if "%_WebDavFilterFileExtensions%"   == "" set _WebDavFilterFileExtensions=false
if "%_WebDavFilterHiddenSegments%"   == "" set _WebDavFilterHiddenSegments=false
if "%_WebDavFilterVerbs%"            == "" set _WebDavFilterVerbs=false
if "%_FtpControlPort%"               == "" set _FtpControlPort=21
if "%_FtpActivePort%"                == "" set _FtpActivePort=20
if "%_DriveName%"                    == "" set _DriveName=f:
if "%_ShareName%"                    == "" set _ShareName=%~4
if "%_SharePath%"                    == "" set _SharePath=%~5

:Paths
call :SetInPath "%windir%\system32\inetsrv"
@echo Additional system paths set.

:Action
goto :%~1
goto :Usage

:Usage
@echo.
@echo Installation of platform features and services in Azure hosting environment.  
@echo.
@echo SETSERVICE [service]
@echo.
@echo   service     Name of the service to enable or install on this role:
@echo.
@echo     Info        Role, Feature and IIS information.
@echo     FTP         File Transfer Protocol.
@echo     WebDav      WebDav.
@echo     DFS         Distributed File System.
@echo.
@goto :eof

REM 
REM ~~( COMBINED )~~
REM 
:Info
call :ListSites
call :ListAppPools
call :ListBackups
call :ServerQuery
@goto :Complete

:EnableFeatures
call :RoleFeatures
@goto :Complete

:WebDav
call :WebDavEnableOnSite
call :WebDavAddAuthoringRule
call :WebDavCompatibility
call :WebDavRequireSSL
call :WebDavAllowHiddenFiles
call :WebDavAllowNonMimeMapFiles
call :WebDavFilterFileExtensions
call :WebDavFilterHiddenSegments
call :WebDavFilterVerbs
@goto :Complete

:Iis
call :Iis_SharedConfigLocation
call :Iis_SharedConfiguration_GrantLogonAsBatchRight
@goto :Complete

:Firewall
call :Firewall_RemoteManagement
call :Firewall_Icmp_Echo
call :Firewall_PowershellRemoting
call :Firewall_Ftp
call :Firewall_Ftp_Ssl
call :Firewall_Ftp_Stateful
call :Firewall_Smb
@goto :Complete

:SMB
call :Firewall_Smb
call :User_CreateAdmin
call :SMB_CreateShare
@goto :Complete

REM 
REM ~~( MANAGE )~~
REM 
:ListSites
AppCmd list site
@goto :eof

:ListAppPools
AppCmd list apppool
@goto :eof

:ListBackups
appcmd list backup
@goto :eof

:ServerQuery
ServerManagerCmd.exe -query
@goto :eof

:CreateBackup
appcmd add backup centralConfigBackup
@goto :eof

:RestoreConfiguration
appcmd restore backup centralConfigBackup 
@goto :eof

:ListRequestFilteringRules
AppCmd list config "%_Site%/" /section:system.webServer/security/requestFiltering 
@goto :eof

REM 
REM ~~( FEATURES )~~
REM 
:RoleFeatures
ServerManagerCmd.exe -inputPath ServerManager.accelerator.xml -resultPath "accel.services.result.xml" -logPath "accel.services.log.txt"
@goto :eof

REM 
REM ~~( WEBDAV )~~
REM 
:WebDavEnableOnSite
AppCmd set config "%_Site%/" /section:system.webServer/webdav/authoring /enabled:true /commit:apphost
@goto :eof

:WebDavAddAuthoringRule
AppCmd set config "%_Site%/" /section:system.webServer/webdav/authoringRules /+[users='%_UserAdmin%',path='*',access='Read,Write,Source'] /commit:apphost
@goto :eof

:WebDavCompatibility
AppCmd set config "%_Site%/" /section:system.webServer/webdav/authoring /compatFlags:"MsAuthorVia,CompactXml,MultiProp,IsHidden,IsCollection" /commit:apphost
@goto :eof

:WebDavRequireSSL
AppCmd set config "%_Site%/" /section:system.webServer/webdav/authoring /requireSsl:%_WebDavRequireSSL% /commit:apphost
@goto :eof

:WebDavAllowHiddenFiles
AppCmd set config "%_Site%/" /section:system.webServer/webdav/authoring /fileSystem.allowHiddenFiles:%_WebDavAllowHiddenFiles% /commit:apphost
@goto :eof

:WebDavAllowNonMimeMapFiles
AppCmd set config "%_Site%/" /section:system.webServer/webdav/authoringRules /allowNonMimeMapFiles:%_WebDavAllowNonMimeMapFiles% /commit:apphost
@goto :eof

:WebDavFilterFileExtensions
AppCmd set config "%_Site%/" /section:system.webServer/security/requestFiltering /fileExtensions.applyToWebDAV:%_WebDavFilterFileExtensions% /commit:apphost
@goto :eof

:WebDavFilterHiddenSegments
AppCmd set config "%_Site%/" /section:system.webServer/security/requestFiltering /hiddenSegments.applyToWebDAV:%_WebDavFilterHiddenSegments% /commit:apphost
@goto :eof

:WebDavFilterVerbs
AppCmd set config "%_Site%/" /section:system.webServer/security/requestFiltering /verbs.applyToWebDAV:%_WebDavFilterVerbs% /commit:appHost
@goto :eof

REM 
REM ~~( IIS )~~
REM 
:Iis_SharedConfigLocation
md %SystemDrive%\centralconfig
net share centralconfig$=%SystemDrive%\centralconfig /grant:Users,Change
@goto :eof

:Iis_SharedConfiguration_GrantLogonAsBatchRight
NtRights.exe -u %_UserSharedConfig% +r SeBatchLogonRight
@goto :eof

REM 
REM ~~( SMB )~~
REM 
:Firewall_Smb
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
netsh advfirewall firewall set rule name="File and Printer Sharing (NB-Session-In)" new enable=yes profile=any
netsh advfirewall firewall set rule name="File and Printer Sharing (NB-Name-In)" new enable=yes profile=any
netsh advfirewall firewall set rule name="File and Printer Sharing (NB-Datagram-In)" new enable=yes profile=any
netsh advfirewall firewall set rule name="File and Printer Sharing (Echo Request - ICMPv4-In)" new enable=yes profile=any
netsh advfirewall firewall add rule name="All ICMP V4" dir=in action=allow protocol=icmpv4
netsh firewall set service type=fileandprint mode=enable profile=Current
@goto :eof

:SMB_CreateShare
echo Share:    %_ShareName%
echo Path:     %_SharePath%
echo User:     %_UserAdmin%
set _Command=net share %_ShareName%='%_SharePath%' /GRANT:%_UserAdmin%,FULL
echo Command: "%_Command%"
call :RunAsAdmin "%_Command%"
%_Command%
goto :eof

REM 
REM ~~( FIREWALL )~~
REM 
:Firewall_ListRules
netsh advfirewall firewall show rule name=all
@goto :eof

:Firewall_RemoteManagement
netsh advfirewall firewall add rule name="Windows Remote Management (HTTP-In)" dir=in action=allow service=any enable=yes profile=any localport=5985 protocol=tcp
@goto :eof

:Firewall_PowershellRemoting
call :Firewall_RemoteManagement

:Firewall_Icmp_Echo
netsh advfirewall firewall add rule name="ICMPv6 echo" dir=in action=allow enable=yes protocol=icmpv6:128,any
@goto :eof

:Firewall_Ftp
netsh advfirewall firewall add rule name="FTP (non-SSL)" action=allow protocol=tcp dir=in localport=%_FtpControlPort%
@goto :eof

:Firewall_Ftp_Ssl
netsh advfirewall firewall add rule name="FTP for IIS" service=ftpsvc action=allow protocol=tcp dir=in
@goto :eof

:Firewall_Ftp_Stateful
netsh advfirewall set global StatefulFtp disable
@goto :eof


REM 
REM ~~( STATE SERVER )~~
REM 


REM 
REM ~~( ADMINISTRATION )~~
REM     
:User_CreateAdmin
net user %_UserAdmin% %_UserAdminPassword% /add
net localgroup Administrators %_UserAdmin% /add
@goto :eof


REM 
REM ~~( UTILITY CALLS )~~
REM
:RunAsAdmin
echo Task to run: "%~1"
schtasks /CREATE /TN "AdminTask" /SC ONCE /SD 01/01/2030 /ST 00:00:00 /RL HIGHEST /RU %_UserAdmin% /RP %_UserAdminPassword% /TR "%~1" /F
schtasks /RUN /TN "AdminTask" /RU %_UserAdmin% /RP %_UserAdminPassword% 
:RunAsAdminWait
@echo Waiting for task to complete...
@ping localhost -n 10 > null
schtasks /QUERY /TN "AdminTask" /V | find /i "AdminTask" && goto RunAsAdminWait
@goto :eof
 
:SetInPath
set %PATH:%~fp1=%
set PATH=%~fp1;%PATH%
goto :eof

:Completed
@echo Completed.

:End
@goto :eof

